package server

import (
	"fmt"
	"github.com/asim/go-micro/plugins/client/grpc/v4"
	"github.com/asim/go-micro/plugins/registry/etcd/v4"
	"github.com/asim/go-micro/plugins/wrapper/select/roundrobin/v4"
	"go-micro.dev/v4"
	"go-micro.dev/v4/client"
	"go-micro.dev/v4/registry"
	clientGrpc "signal/client/grpc"
	"signal/config"
	grpcServer "signal/server/grpc"
	"signal/server/grpc/middleware"
	tcpServer "signal/server/tcp"
)

func Init() func() {
	fmt.Println("----------------------- server init --------------")
	config.MicroService = micro.NewService()

	config.MicroService.Init(
		micro.Registry(etcd.NewRegistry(registry.Addrs(config.EtcdAddr))),
		micro.BeforeStart(BeforeStart),
		micro.BeforeStop(BeforeStop),
		micro.AfterStart(AfterStart),
		micro.AfterStop(AfterStop),
		// client trace wrapper
		micro.Client(grpc.NewClient(client.WrapCall(middleware.ClientTraceWrapper()))),
		// choose you needed wrapper
		micro.WrapClient(roundrobin.NewClientWrapper()),
		//webServer.Init(),
		grpcServer.Init(),
	)

	clientGrpc.Init()
	tcpServer.Init()
	return func() {

	}
}

func BeforeStart() error {
	fmt.Println("BeforeStart start success")
	return nil
}

func BeforeStop() error {
	fmt.Println("BeforeStop start success")
	return nil
}

func AfterStop() error {
	fmt.Println("AfterStop start success")
	return nil
}

func AfterStart() error {
	fmt.Println("AfterStart start success")
	return nil
}
